package com.houweishi.module.wageOperate.controller;

import com.houweishi.common.annotation.Log;
import com.houweishi.common.core.controller.BaseController;
import com.houweishi.common.core.domain.AjaxResult;
import com.houweishi.common.core.page.TableDataInfo;
import com.houweishi.common.enums.BusinessType;
import com.houweishi.module.wageOperate.domain.RenewalSearchVO;
import com.houweishi.module.wageOperate.domain.WageOperate;
import com.houweishi.module.wageOperate.service.IWageOperateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工资计算Controller
 * 
 * @author yqq
 * @date 2023-06-20
 */
@RestController
@RequestMapping("/module/wageOperate")
public class WageOperateController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(WageOperateController.class);
    @Autowired
    private IWageOperateService wageOperateService;

    /**
     * 查询工资计算列表
     */
    @PreAuthorize("@ss.hasPermi('module:wageOperate:list')")
    @GetMapping("/list")
    public TableDataInfo list(WageOperate wageOperate)
    {
        startPage();
        List<WageOperate> list = wageOperateService.selectWageOperateList(wageOperate);
        return getDataTable(list);
    }

    /**
     * 计算工资合计
     */
     @PostMapping("/salary/sum")
     public AjaxResult calcSalarySum(@RequestBody RenewalSearchVO renewalSearchVO){
         return wageOperateService.calcSalarySum(renewalSearchVO);
     }


    /**
     * 导出工资计算列表
     */
    @PreAuthorize("@ss.hasPermi('module:wageOperate:export')")
    @Log(title = "工资计算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RenewalSearchVO renewalSearchVO)
    {
        wageOperateService.export(response,renewalSearchVO);
    }

    /**
     * 获取工资计算详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:wageOperate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wageOperateService.selectWageOperateById(id));
    }

    /**
     * 新增工资计算
     */
    @PreAuthorize("@ss.hasPermi('module:wageOperate:add')")
    @Log(title = "工资计算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WageOperate wageOperate)
    {
        return toAjax(wageOperateService.insertWageOperate(wageOperate));
    }

    /**
     * 更新工资计算表基本数据
     */
    @Log(title = "工资计算", businessType = BusinessType.OTHER)
    @GetMapping("/data/renewal")
    public AjaxResult renewalData()
    {
        try {
            return wageOperateService.renewalData();
        } catch (Exception e) {
            LOGGER.error("更新失败",e);
            return AjaxResult.error("更新失败，请联系管理员");
        }
    }

    /**
     * 计算工资计算表考勤
     */
    @Log(title = "工资计算", businessType = BusinessType.OTHER)
    @PostMapping("/clocking/renewal")
    public AjaxResult renewalClocking(@RequestBody RenewalSearchVO renewalSearchVO)
    {
        try {
            return wageOperateService.renewalClocking(renewalSearchVO);
        } catch (Exception e) {
            LOGGER.error("计算工资计算表考勤失败",e);
            return AjaxResult.error("计算工资计算表考勤失败，请联系管理员");
        }
    }

    /**
     * 计算工资
     */
    @Log(title = "计算工资", businessType = BusinessType.OTHER)
    @PostMapping("/compute/salary")
    public AjaxResult calcSalary(@RequestBody RenewalSearchVO renewalSearchVO)
    {
        try {
            return wageOperateService.calcSalary(renewalSearchVO);
        } catch (Exception e) {
            LOGGER.error("计算薪资失败",e);
            return AjaxResult.error("计算薪资失败，请联系管理员");
        }
    }

    /**
     * 修改工资计算
     */
    @PreAuthorize("@ss.hasPermi('module:wageOperate:edit')")
    @Log(title = "工资计算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WageOperate wageOperate)
    {
        return toAjax(wageOperateService.updateWageOperate(wageOperate));
    }

    /**
     * 删除工资计算
     */
    @PreAuthorize("@ss.hasPermi('module:wageOperate:remove')")
    @Log(title = "工资计算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wageOperateService.deleteWageOperateByIds(ids));
    }

    /**
     * 保存工资历史记录
     * @return
     */
    @PostMapping("/save/history")
    public AjaxResult saveWageOperateHistory(@RequestBody RenewalSearchVO renewalSearchVO){
        try {
            return wageOperateService.saveWageOperateHistory(renewalSearchVO);
        } catch (Exception e) {
            LOGGER.error("保存工资历史记录失败",e);
            return AjaxResult.error("保存工资历史记录失败，请联系管理员");
        }
    }

    /**
     * 公开当月薪资
     * @return
     */
    @PostMapping("/wage/public")
    public AjaxResult publicToStaff(@RequestBody RenewalSearchVO renewalSearchVO){
        try {
            return wageOperateService.publicToStaff(renewalSearchVO);
        } catch (Exception e) {
            LOGGER.error("公开当月薪资录失败",e);
            return AjaxResult.error("公开当月薪资失败，请联系管理员");
        }
    }
}
